#!/usr/sbin/rsct/perl5/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#
# @(#)23   1.11         src/rsct/cfg_access/gpfs/ct_hats_info.perl, cfg.access, rsct_rzauh, rzauh0431a 1/25/03 13:55:10
#
# This will give the hats subsystem information for GPFS clusters
# Usage: ct_hats_info [-l] [-w]
#    -l  to get the data from the local repository (performance)
#    -w  to set the data to the repository (input from stdin)
# Output:
#	REALM		CLUSTER
#	PORT 		<port number>
#	FIXED_PRI	<value>
#	PIN		<value>
#	LOGFILELEN	<value>
#
use Getopt::Std;

my %opts = ();
&getopts(':lw',\%opts);
my $mmrefresh=(defined($opts{l}) ? "norefresh" : "");
my $mmsetting=(defined($opts{w}) ? 1 : 0);

$RSCTBIN="/usr/sbin/rsct/bin";
$MMHA="/usr/lpp/mmfs/bin/mmha";

($dir,$progname) = $0 =~ /(.*\/)?(.*)/; # get basename of $0

# ValidKeys: list of all acceptable keys (either canset or not).
# In other words, the input keys must be in the list.
# Otherwise, an error will be issued.

$NON_SETTABLE = 0;
$SETTABLE = 1;

%ValidKeys = (
    "ENVIRONMENT"       => $NON_SETTABLE,
    "START_AT_BOOT"      => $NON_SETTABLE,
    "RESTART_BY_SRC"      => $NON_SETTABLE,
    "REALM"             => $NON_SETTABLE,
    "PORT"              => $SETTABLE,
    "FIXED_PRI"     	=> $SETTABLE,
    "LOGFILELEN"        => $SETTABLE,
    "PIN"		=> $SETTABLE
);

# main starts here
if($mmsetting) {
   #write the data to the repository
   &print_dbgmsg("Set data to Repository");
   &set_data_to_repository();
} else {
   # show the environment and AutoStart flags
   print "ENVIRONMENT    gpfs\n";
   print "START_AT_BOOT  1\n";
   print "RESTART_BY_SRC 1\n";

   my $outcnt = &get_data_from_repository();
   if($outcnt == 0) {
	&print_dbgmsg("No fields are read.");
        exit(1);
   }
}
exit(0);

#-------- functions -----------------------------------
# print_dbgmsg(list)
sub print_dbgmsg
{
  my @args = @_;
  if(!defined($ccal_log_inited)) {
	my $outfile = $ENV{"HA_CCAL_LOG"};
	$ccal_log_inited = 0;
	if(defined($outfile) && open(LOGOUT, ">>$outfile")) {
	    $ccal_log_inited = 1;
	    select LOGOUT;
	    $| = 1; # unbuffered
	    select STDOUT;
	}
  }
  if($ccal_log_inited) {
	my $dstr = scalar(localtime);
	my $msg = "$dstr $progname: " . join(" ", @args);
	print LOGOUT $msg, "\n" ;
  }
}

sub print_errmsg
{
   &print_dbgmsg(@_);
   print STDERR "# $progname: ", join(" ", @_), "\n";
}
#-----------------------------------------------------

# GET DATA from the repository
sub get_data_from_repository
{
   my $outcnt = 0;
   $MMHATSCMD="${MMHA} haGetHatsInfo all $mmrefresh";
   &print_dbgmsg("$MMHATSCMD");
   if(!open(MMCMDFILE, "${MMHATSCMD} |")) {
	&print_errmsg("Error on '${MMHATSCMD}'");
	exit(1);
   }

   local($line);
   my @pair = ();
   while($line = <MMCMDFILE>) {
	&print_dbgmsg("Rep: $line");
	my %mmFields = &trans_mmha_to_hash($line);
	while( @pair = each %mmFields ) {
 	    print "$pair[0] $pair[1]\n";
	    $outcnt++;
	}
   }
   close MMCMDFILE;
   return $outcnt;
}


# SET DATA to the repository 
sub set_data_to_repository
{
   my $count = 0;
   my %kvFields = ();
   my @fields;
   my ($key, $val, $canset);
   $MMHAPUTCMD="$MMHA haPutHatsInfo";
   while( <STDIN> ) {
	@fields = split " ";	# split input
	$key = $fields[0];
	$val = join(" ",@fields[1..$#fields]);
	next if(!defined($key) || ($key eq "") || ($key =~ /^#/));
        $key = uc $key;		# upper case
	$canset = $ValidKeys{$key};	
	if(defined($canset)) {
	    # process the field
	    if($canset == $SETTABLE) {
		&print_dbgmsg("Setting field: $key $val");
		$kvFields{$key} = $val;
		$count++;
	    } else {
	    	&print_dbgmsg("Unsettable field: $key $val");
	    }
	} else {
	    # not defined...unknown key
	    &print_errmsg("Unknown field: $key $val");
	}
   }
   
   if($count == 0) {
	#no fields will be updated
	return 0;
   }

   # merge the 'kvFields' as the colon-separated list
   # For example, KA=V1:KB=V2:
   my @mmInList = ();
   while( ($key,$val) = each %kvFields ) {
	if($val eq "") { $val = " "; }	# make sure a 'blank' exists
	my $kvStr = join("=",($key,$val));
	push @mmInList, ($kvStr);
   }
   my $mmInStr = join(":",@mmInList);
   &print_dbgmsg("Update: $mmInStr");
   
   #update the inputs
   `$MMHAPUTCMD \"$mmInStr\"`;
   $rc = $?;
   &print_dbgmsg("$MMHAPUTCMD $mmInStr RETURNS $rc");
   
   return $count;
}


#
# Input:  a colon-separated mmha output line
#         eg: KA=V1:KB=V2:KC=V3:
# Output: hash table with pairs of {K}={V}
sub trans_mmha_to_hash
{
    my $parm = $_[0];
    chomp($parm);
    my @fields = split(":",$parm);
    my %outtbl = ();
    foreach $item (@fields) {
	my @kv=split("=",$item);
	if(scalar(@kv) >= 2) {
            $outtbl{$kv[0]} = join("=",@kv[1..$#kv]);
	} elsif(scalar(@kv) > 0) {
	    &print_dbgmsg("Unkown field $item");
	}
    }
    return %outtbl;
}

